package edu.gatech.oad.schedule;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import javax.swing.DefaultListCellRenderer;
import javax.swing.DefaultListModel;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.ListCellRenderer;
import javax.swing.border.TitledBorder;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

import edu.gatech.oad.records.TreatmentRecord;

public class CheckedInPanel extends JPanel{

	private static final long serialVersionUID = 1L;
	private JList todaysAppointments;
	private List<Appointment> todaysAppt = CalendarController.sharedCalendarController().getAppointmentsThatDay();
	private List<String> apptTimes = new ArrayList<String>();
	private DefaultListModel defaultListModel = new DefaultListModel();
	
	public CheckedInPanel(){
		getAppointmentsTitles();
		this.setPreferredSize(new Dimension(200,800));
		this.setBackground(Color.white);
		Date date = Calendar.getInstance().getTime();
		DateFormat df = new SimpleDateFormat("M/d/y");
		String today = df.format(date);
		this.setBorder(new TitledBorder("Today's Appointment: " + today ));
		this.setLayout(new BorderLayout());
		todaysAppointments = new JList(defaultListModel);
		for(String appString : apptTimes){
			defaultListModel.addElement(appString);
		}
		
		todaysAppointments.setBackground(Color.WHITE);
		todaysAppointments.addMouseListener(new ListClickListener());
		todaysAppointments.setPreferredSize(new Dimension(150, 500));
		ListCellRenderer renderer = new StatusListRenderer();
		todaysAppointments.setCellRenderer(renderer);

		add(todaysAppointments, BorderLayout.CENTER);
	}

	public void getAppointmentsTitles(){
		Calendar cal = Calendar.getInstance();
		for(Appointment appt : todaysAppt){
			cal.setTime(appt.getDate());
			DateFormat df = new SimpleDateFormat("h:mm");
			String date = df.format(cal.getTime());
			apptTimes.add(appt.getPatient().getName() + " - " + date);
		}
	}

	private class ListClickListener extends MouseAdapter{
		private static final int MAX_CLICKS = 2;
		public void mouseClicked(MouseEvent evt) {
	        final JList list = (JList) evt.getSource();
	        if (evt.getClickCount() == MAX_CLICKS) {
	        	final int index = list.locationToIndex(evt.getPoint());
	        	final Appointment appointment = todaysAppt.get(index);
	        	if(index != -1){
	        		if(!appointment.isCheckedIn()){
	        			final int option = JOptionPane.showConfirmDialog(null, 
	        					"Would You Like to Check In " + appointment.getPatient().getName() +
	        					" for today's appointment?", "Check In" + appointment.getPatient().getName(),
	        					JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
	        			if(option == JOptionPane.YES_OPTION){
	        				appointment.changeCheckIn();
	        				if(appointment.isCheckedIn()){
	        					TreatmentRecord treatmentRecord = new TreatmentRecord(appointment);
	        					appointment.getPatient().getPatientHistory().addTreatmentRecord(treatmentRecord);
	        				}
	        			}
	        		}else if(appointment.isCheckedIn()){
	        			if(!appointment.isTreated()){
	        				final int option = JOptionPane.showConfirmDialog(null, 
		        					"Was " + appointment.getPatient().getName() +
		        					" treated and ready for checkout?", "Check Out " + appointment.getPatient().getName(),
		        					JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
		        			if(option == JOptionPane.YES_OPTION){
		        				appointment.changeTreated();
		        				defaultListModel.removeElementAt(index);
		        			}
	        			}
	        		}
	        	}
        	} 
	    }
	}
	
	private class StatusListRenderer implements ListCellRenderer {
		  protected DefaultListCellRenderer defaultRenderer = new DefaultListCellRenderer();
		  public Component getListCellRendererComponent(JList list, Object value, int index,
		      boolean isSelected, boolean cellHasFocus) {
		    Font theFont = null;
		    Color theForeground = null;
		    Icon theIcon = null;
		    String theText = null;

		    JLabel renderer = (JLabel) defaultRenderer.getListCellRendererComponent(list, value, index,
		        isSelected, cellHasFocus);

		    renderer.setText((String) value);
		    renderer.setIcon(new MyIcon(index));
		    return renderer;
		  }
		}

	
	private class MyIcon implements Icon {
		private int selectedIndex;
		public MyIcon(int selectedIndex) {
			this.selectedIndex = selectedIndex;
		}
		public int getIconHeight() {
			return 20;
		}
		public int getIconWidth() {
			return 20;
		}
		public void paintIcon(Component c, Graphics g, int x, int y) {
			if(todaysAppt.get(selectedIndex).isCheckedIn()){
				g.setColor(Color.green);
			} else {
				g.setColor(Color.RED);
			}
			g.fillOval(0, 0, 20, 20);
		}
	}
}
